package com.draw.cli.gui.panel.home;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.xml.ws.WebServiceException;

import com.draw.cli.config.ConfigManager;
import com.draw.cli.gui.Window;
import com.draw.cli.gui.panel.Refreshable;
import com.draw.cli.gui.panel.UserTable;
import com.draw.cli.gui.panel.play.PlayPanel;
import com.draw.cli.webclient.DrawingClient;
import com.draw.core.Drawing;
import com.draw.core.User;
import com.draw.webinterface.DrawingInterface;

public class WaitingDrawPanel extends JPanel implements Refreshable,
		ActionListener, ListSelectionListener , Runnable {
	private static final long serialVersionUID = 6015939217880763417L;

	private static WaitingDrawPanel instance = new WaitingDrawPanel();

	public static WaitingDrawPanel getInstance() {
		return instance;
	}

	private List<Drawing> draws;
	// private DefaultListModel<User> model;
	// private JList<User> idents;
	// private JButton button;

	private JButton button;
	private UserTable table;

	public WaitingDrawPanel() {
		this.setBorder(BorderFactory.createTitledBorder("Dessins en attente"));
		this.setLayout(new BorderLayout());
		// this.model = new DefaultListModel<User>();
		// this.idents = new JList<User>(this.model);
		// this.idents.addListSelectionListener(this);
		// JScrollPane scroll = new JScrollPane(this.idents);
		// this.add(scroll);
		this.table = new UserTable();
		this.table.getTable().getSelectionModel()
				.addListSelectionListener(this);
		this.table.getTable().getColumnModel().getSelectionModel()
				.addListSelectionListener(this);
		this.add(this.table);
		this.button = new JButton("Visionner");
		this.button.addActionListener(this);
		this.button.setEnabled(false);
		this.add(this.button, BorderLayout.SOUTH);
	}

	@Override
	public void refresh() {
		// this.model.clear();
		new Thread(this).start();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		this.button.setEnabled(false);
		User user = this.table.getSelectedUser();
		int index;
		for (index = 0; index < this.draws.size()
				&& !this.draws.get(index).getSource().equals(user); index++)
			;
		if (index >= 0 && index < this.draws.size()) {
			PlayPanel.getInstance().initialize(this.draws.get(index));
			Window.getInstance().setPlay();
		}
	}

	@Override
	public void valueChanged(ListSelectionEvent arg0) {
		if (this.table.getSelectedUser() != null) {
			this.button.setEnabled(true);
		}
	}

	@Override
	public void run() {
		ArrayList<User> users = new ArrayList<User>();
		this.table.setUsers(users);
		users = new ArrayList<User>();
		User user = ConfigManager.getInstance().getCurrentUser();
		if (user != null) {
			try {
				DrawingInterface di = DrawingClient.getInstance();
				this.draws = di.getDrawingReceived(user);
				for (Drawing draw : this.draws) {
					// this.model.addElement(draw.getSource());
					users.add(draw.getSource());
				}
			} catch (MalformedURLException | WebServiceException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		this.table.setUsers(users);
		this.table.getTable().repaint();
	}
}
